Repository navigation
purego: fix ARM callback trampoline spacing below ARMv7 - #563
Conversation
Split callback indices into two encodable immediates to prevent ARMv5 literal pools from breaking trampoline address calculation. Use a twelve-byte stride and regenerate the assembly table. Verify the instructions, loaded callback index, and branch destination for all 2,000 entries without consuming callback registrations.
hajimehoshi
left a comment
There was a problem hiding this comment.
Posted by Claude (Claude Code) on behalf of hajimehoshi. All of these are suggestions, and none is a confirmed failure. The PR's code was not built or run for this review; the findings come from reading the code and assembling zcallback_arm.s with go tool asm.
GOARM=6 needs the split MOVW/ORR load too: only GOARM=7 gets the 16-bit MOVW, so describe the affected targets as below ARMv7. Rework the ARM encoding test as an external test in callback_linux_arm_test.go. It now checks that every branch reaches callbackasm1, not only that all branches agree, so a shared wrong destination such as a linker trampoline would fail. It reports every bad entry, up to a limit, instead of stopping at the first one, and drops the stride check, which only compared two hard-coded constants. Add TestCallbackEntries, which re-executes the test binary, registers maxCB callbacks that each return their own index, and calls them all. This covers every architecture without depending on the instruction encodings, and leaves the callback slots of the parent process untouched. It is skipped when the binary cannot be re-executed, such as under a user-mode emulator without binfmt_misc.
|
Fix the test failure. |
On the arm64 CI runner the linux/arm child failed to start with "libdl.so.2: cannot open shared object file". QEMU_LD_PREFIX only tells qemu-user where the target's shared libraries are, but the child is not always run by the same emulator: an arm64 host with AArch32 support runs it natively, and the dynamic loader then searches the host's directories. When QEMU_LD_PREFIX is set, also pass its lib directory in LD_LIBRARY_PATH to the child. Under qemu-user the path resolves to the same libraries.
The arm64 runner can execute linux/arm binaries natively. The child process of TestCallbackEntries therefore runs outside QEMU, and the kernel looks for the cgo test binary's dynamic linker, /lib/ld-linux-armhf.so.3, on the host, where only /lib/ld-linux.so.3 was linked. Link it as well.
|
Fixed in a9b1911 and 628c659. The arm64 runner executes linux/arm binaries natively, so the child process of
The full workflow passes on my fork: https://github.com/yuhaiin/purego/actions/runs/42329129160. In the |
hajimehoshi
left a comment
There was a problem hiding this comment.
Posted by Claude (Claude Code) on behalf of hajimehoshi. This is a suggestion, and it comes from the CI job logs; the PR's code was not built or run for this review.
…ctures binfmt_misc is not registered on the runner that tests loong64, ppc64le, riscv64 and s390x under QEMU, so TestCallbackEntries cannot re-execute the test binary and skips. Run its child mode directly after each test binary so these callback tables are exercised too.
|
I'm sorry but the squashed commit message included |
What issue is this addressing?
On Linux/ARM below ARMv7 (
GOARM=5andGOARM=6), loading a callback indexwith
MOVWcan require a literal pool when the value cannot be encoded as arotated immediate. Only
GOARM=7gets the 16-bitMOVW. The assembler insertsthese pools into
callbackasm, whilecallbackasmAddrassumes every entryoccupies exactly eight bytes. After enough callback registrations, the computed
callback address can point into literal data and cause an illegal instruction.
This is the ARM correctness fix separated from #562 as requested in
the review discussion.
It applies directly to current main and contains no callback-dispatch optimization.
What type of issue is this addressing?
Bug.
What this PR does | solves
Build each callback index from two encodable immediates using
MOVWandORR,followed by the branch. Update the ARM entry stride to twelve bytes and regenerate
the trampoline table with
go generate. All ARM targets use the same layout.The large assembly diff is generated; the handwritten production changes are in
the generator and address calculation.
Add two tests:
TestCallbackEntriesre-executes the test binary, registersmaxCBcallbacksthat each return their own index, and calls them all. It covers every
architecture with callback support, does not depend on instruction encodings,
and leaves the parent's callback slots untouched. It is skipped when the binary
cannot be re-executed, such as under a user-mode emulator without binfmt_misc.
TestCallbackTrampolineEncoding(linux/arm) checks all entries withoutregistering callbacks. It verifies the instruction opcodes, decodes the rotated
immediates to check the callback index, and checks that every branch reaches
callbackasm1. It reports every bad entry, up to a limit.Validation
pass.
TestCallbackEntrieswas run in child mode directly there, because thelocal QEMU setup has no binfmt_misc and the parent skips.
TestCallbackTrampolineEncodingfailsfrom entry 0 and
TestCallbackEntriesfails with wrong indices(for example, callback 445 returns 444).
go vetpasses with CGO enabled and disabled,gofmtis clean, and rerunninggo generate ./...leaves the committed tree unchanged.